programming4us
           
 
 
Windows

Windows 7 : Working with the Command-Line Tools (part 1) - Working with Disk Management Tools

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/7/2010 11:19:13 AM
The real power of the command line shines through when you combine the techniques you’ve learned so far with any of Windows 7’s dozens of command-line tools. I don’t have enough space to cover every tool, so the rest of this article takes you through the most useful and powerful command-line tools in three categories: disk management, file management, and system management.

Working with Disk Management Tools

Windows 7 comes with a large collection of command-line disk management tools that enable you to check disks or partitions for errors, as well as defragment, format, partition, and convert disks. Table 1 lists the disk management tools that you can use with Windows 7.

Table 1. Windows 7’s Command-Line Disk Management Tools
ToolDescription
CHKDSKChecks a specified volume for errors.
CHKNTFSConfigures automatic disk checking.
CONVERTConverts a specified volume to a different file system.
DEFRAGDefragments a specified volume.
DISKCOMPCompares the contents of two floppy disks. (This tool does not compare hard disks or other types of removable media, such as memory cards.)
DISKCOPYCopies the contents of one floppy disk to another. (This tool does not copy hard disks or other types of removable media, such as memory cards.)
DISKPARTEnables you to list, create, select, delete, and extend disk partitions.
EXPANDExtracts one or more files from a compressed file such as a .cab file found on some installation discs.
FORMATFormats the specified volume.
FSUTILPerforms a number of file system tasks.
LABELChanges or deletes the name of a specified volume.
MOUNTVOLCreates, displays, or deletes a mount point.
VOLDisplays the name and serial number of a specified volume.

Note

In this section, I use the word volume to refer to any disk, partition, or mount point.


The next four sections give you more detailed coverage of the CHKDKS, CHKNTFS, and DEFRAG tools.

CHKDSK: Checking for Hard Disk Errors

Check Disk also comes with a command-line version called CHKDSK that you can run in a Command Prompt window.

Here’s the syntax for CHKDSK:

CHKDSK [volume [filename]] [/F] [/V] [/R] [/B] [/X] [/I] [/C] [/L:[size]]

volumeThe drive letter (followed by a colon) or mount point.
filenameOn FAT16 and FAT32 disks, the name of the file to check. Include the path if the file isn’t in the current folder.
/FTells CHKDSK to automatically fix errors. This is the same as running the Check Disk GUI with the Automatically Fix File System Errors option activated.
/VRuns CHKDSK in verbose mode. On FAT16 and FAT32 drives, CHKDSKCHKDSK displays the path and name of every file on the disk; on NTFS drives, displays cleanup messages, if any.
/RTells CHKDSK to scan the disk surface for bad sectors and recover data from the bad sectors, if possible. (The /F switch is implied.) This is the same as running the Check Disk GUI with the Scan for and Attempt Recovery of Bad Sectors option activated.
/BTells CHKDSK to clear the list of bad sectors on the disk and then recheck the entire disk. Including this parameter is the same as also including the /R parameter.
/XOn NTFS nonsystem disks that have open files, forces the volume to dismount, invalidates the open file handles, and then runs the scan. (The /F switch is implied.)
/IOn NTFS disks, tells CHKDSK to check only the file system’s index entries.
/COn NTFS disks, tells CHKDSK to skip the checking of cycles within the folder structure. This is a rare error, so using /C to skip the cycle check can speed up the disk check.
/L:[size]On NTFS disks, tells CHKDSK to set the size of its log file to the specified number of kilobytes. The default size is 65,536, which is plenty big enough for most systems, so you should never need to change the size. Note that if you include this switch without the size parameter, Check Disk tells you the current size of the log file.

For example, to run a read-only check—that is, a check that doesn’t repair errors—on the C: drive, you enter the following command:

chkdsk c:

Note that when you use the /F switch to fix errors, CHKDSK must lock the volume to prevent running processes from using the volume during the check. If you use the /F switch on the %SystemDrive%, which is the drive where Windows 7 is installed (usually drive C:), CHKDSK can’t lock the drive, and you see the following message:

Cannot lock current drive.


Chkdsk cannot run because the volume is in use by another
process. Would you like to schedule this volume to be
checked the next time the system restarts? (Y/N)

If you press Y and Enter, CHKDSK schedules a check for drive C: to run the next time you reboot Windows 7.

CHKNTFS: Scheduling Automatic Disk Checks

You saw in the preceding section that CHKDSK prompts you to schedule an automatic disk check during the next reboot if you run CHKDSK /F on the system drive (usually drive C: in Windows 7).

If you press Y and Enter at these prompts, CHKDSK adds the AUTOCHK utility to the following Registry setting:

HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\BootExecute

This setting specifies the programs that Windows 7 should run at boot time when the Session Manager is loading. AUTOCHK is the automatic version of CHKDSK that runs at system startup.

Windows 7 also comes with a command-line tool named CHKNTFS that enables you to cancel pending automatic disk checks, schedule boot-time disk checks without using CHKDSK, and set the time that AUTOCHK counts down before running the automatic disk checks.

Here’s the syntax for CHKNTFS:

CHKNTFS [volume ][/C volume:] [/X volume:] [/D] [/T:[time]]

volumeA drive letter (followed by a colon) or mount point.
/C volumeTells CHKNTFS to schedule an automatic startup disk check for the specified volume. You can specify multiple volumes (separated by spaces).
/X volumeTells CHKNTFS to exclude the specified volume from an automatic startup disk check. You can specify multiple volumes (separated by spaces).
/DTells CHKNTFS to exclude all volumes from an automatic startup disk check.
/T:[time]Specifies the time that AUTOCHK counts down before starting the automatic disk checks.

When you run CHKNTFS with just a volume name, you see one of the following:

  • If the volume is not scheduled for a startup disk check, you see the volume’s file system:

    The type of the file system is NTFS.
  • If the volume is scheduled for a startup disk check, you see the following message:

    Chkdsk has been scheduled manually to run on next reboot.
  • If Windows 7’s Storage Manager has detected an error on the volume, it marks the volume as dirty, so in this case, you see the following message (using drive C: as an example):

    C: is dirty. You may use the /C option to schedule chkdsk for this drive.

This last message is confusing because Windows 7 always performs an automatic startup disk check of any volume that’s marked as dirty. What you can do with CHKNTFS is bypass the automatic startup disk check of any volume that is marked as dirty. To do that, run CHKNTFS with the /X switch, as in this example:

chkntfs /x c:

Note

To manually mark a volume as dirty, use the FSUTIL DIRTY SET volume command, where volume is the drive you want to work with. For example, the following command marks drive C: as dirty:

fsutil dirty set c:

If you’re not sure whether a drive is dirty, either run CHKNTFS volume or run FSUTIL DIRTY QUERY volume, as in this example:

fsutil dirty query c:

Note, however, that FSUTIL doesn’t give you any way to unmark a drive as dirty.


If a volume isn’t already marked as dirty, you can force CHKDSK to check a volume at startup by running CHKNTFS with the /C switch. For example, the following command sets up an automatic start check for the D: drive:

chkntfs /c d:

Note that the /C switch is cumulative, meaning that if you run it multiple times and specify a different volume each time, CHKNTFS adds each new volume to the list of volumes to check at startup. Instead of running multiple commands, however, you can specify multiple volumes in a single command, like so:

chkntfs /c c: d:

If you know a volume has been scheduled for a startup check, but you want to cancel that check, run CHKNTFS with the /X switch, as in this example:

chkntfs /x d:

You can also specify multiple volumes, if needed:

chkntfs /x c: d:

If you know that multiple volumes are scheduled for automatic startup checks, you can cancel all the checks by running CHKNTFS with the /D switch:

chkntfs /d

If you’ve scheduled a startup check for one or more volumes, or if a volume is marked as dirty, the next time you reboot Windows 7, you see a message similar to the following (which uses drive C: as an example):

Checking file system on C:
The type of the file system is NTFS.
Volume label is SYS.

One of your disks needs to be checked for consistency. You
may cancel the disk check, but it is strong recommended
that you continue.
To skip disk checking, press any key within 10 second(s).

The number of seconds in the last line counts down to 0: If you press a key before the countdown ends, Windows 7 skips the disk check; otherwise, it continues with CHKDSK.

Caution

Pressing any key to skip the disk check usually only works with wired keyboards. On most wireless keyboards, pressing a key has no effect.


You can change the initial countdown value by running CHKNTFS with the /T switch, followed by the number of seconds you want to use for the countdown. For example, the following command sets the countdown to 30 seconds:

chkntfs /t:30

Note that if you run the command CHKNTFS /T (that is, you don’t specify a countdown value), CHKNTFS returns the current countdown value.

DEFRAG: Defragmenting the System Drive

If you want to schedule a defragment or perform this chore from a batch file, you have to use the DEFRAG command-line tool. Here’s the syntax:

DEFRAG disks[/a] [/c] [/e] [/f] [/h] [/r] [/t] [/u] [/v]

disksSpecifies the drive letter (followed by a colon) of each disk you want to defragment (separate multiple drives with a space).
/aTells DEFRAG only to analyze the disk.
/cTells DEFRAG to defragment all the system’s drives.
/eTells DEFRAG to defragment all the system’s drives except those specified with the disks parameter.
/fForces DEFRAG to defragment the disk, even if it doesn’t need defragmenting or if the disk has less than 7% free space. (DEFRAG normally requires at least that much free space because it needs an area in which to sort the files.)
/hRuns DEFRAG with a higher program priority for better performance.
/rTells DEFRAG to defragment all the specified drives at the same time (in parallel).
/tTells DEFRAG to show the progress of an already running defrag.
/uTells DEFRAG to show the progress of the defrag.
/vRuns DEFRAG in verbose mode, which displays both the analysis report and the defragmentation report.

Note

To run the DEFRAG utility, you must use an administrator Command Prompt session, as described earlier.


Caution

The DEFRAG switches are case sensitive. So, for example, the following command will work properly:

defrag c: -a

However, this command will not:

defrag c: -A


For example, to get an analysis report of the fragmentation of drive C:, enter the following command:

defrag c: -a

If the volume isn’t too fragmented, you see a report similar to this:

Post Defragmentation Report:

Volume Information:
Volume size = 116.49 GB
Free space = 106.89 GB
Total fragmented space = 1%
Largest free space size = 56.49 GB

Note: File fragments larger than 64MB are not
included in the fragmentation statistics.

You do not need to defragment this volume.

However, if the drive is quite fragmented, you see a report similar to the following:

Post Defragmentation Report:

Volume Information:
Volume size = 397.12 GB
Free space = 198.32 GB
Total fragmented space = 9%
Largest free space size = 158.43 GB
Note: File fragments larger than 64MB are not
included in the fragmentation statistics.

You should defragment this volume.

If you try to defragment a volume that is running low on disk space, DEFRAG displays the following message:

Volume DATA has only 9% free space available for use by Disk Defragmenter.
To run effectively, Disk Defragmenter requires at least 15% usable free space.
There is not enough disk space to properly complete the operation.
Delete some unneeded files on your hard disk, and then try again.

If you can’t delete files from the volume (for example, if this is a Windows 7 data partition), you can try running DEFRAG with the -f switch to force the operation:
defrag d: -f

Note

Forcing the defrag operation shouldn’t cause problems in most cases. With less free space in which to work, DEFRAG just takes quite a bit longer to defragment the volume, and there may be parts of the volume that it simply can’t defragment.

Other -----------------
- SOA with .NET and Windows Azure : System.Transactions
- Windows 7 : Understanding Batch File Basics (part 2) - Using Batch File Parameters
- Windows 7 : Understanding Batch File Basics (part 1) - Creating Batch Files
- Discovering the Microsoft Azure Platform
- SOA with .NET and Windows Azure : Microsoft Messaging Queue (MSMQ)
- Windows 7 : Working at the Command Line (part 3)
- Windows 7 : Working at the Command Line (part 2)
- Windows 7 : Working at the Command Line (part 1)
- Windows 7 : Getting to the Command Line (part 2) - Running CMD
- Windows 7 : Getting to the Command Line (part 1)
- Windows Azure : Programming Access Control Service (part 10) - Deploying the Web Service in Windows Azure
- Windows Azure : Programming Access Control Service (part 9) - Configuring a Web Service Client to Acquire and Send SAML Tokens
- Windows Azure : Programming Access Control Service (part 8)
- Windows Azure : Programming Access Control Service (part 7) - Integrating ACS with a SAML Token Provider
- Windows Azure : Programming Access Control Service (part 6)
- Windows Azure : Programming Access Control Service (part 5)
- Windows Azure : Programming Access Control Service (part 4)
- Windows Azure : Programming Access Control Service (part 3)
- Windows Azure : Programming Access Control Service (part 2)
- Windows Azure : Programming Access Control Service (part 1)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us